﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IniHelper;

namespace GlobalConfigFactory
{
    public partial class GlobalConfigWindow : Form
    {
        private IniContainer infoContainer;

        public GlobalConfigWindow(IniContainer container)
        {
            InitializeComponent();
            
            this.infoContainer = container;
        }

        private void btnFrmInfoChoose_Click(object sender, EventArgs e)
        {
            OpenFileDialog handwritingFileDialog = new OpenFileDialog();
            handwritingFileDialog.InitialDirectory = tbFrmInfo.Text;
            handwritingFileDialog.Filter = "Config File (*.ini)|*.ini";
            if (handwritingFileDialog.ShowDialog() == DialogResult.OK)
            {
                tbFrmInfo.Text = handwritingFileDialog.FileName;

                infoContainer.overwriteIniValue("WebFile", "FrmInfo", handwritingFileDialog.FileName);
            }
        }

        private void btnProInfoChoose_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog handwritingFolderDialog = new FolderBrowserDialog();
            handwritingFolderDialog.SelectedPath = tbProInfo.Text;
            if (handwritingFolderDialog.ShowDialog() == DialogResult.OK)
            {
                tbProInfo.Text = handwritingFolderDialog.SelectedPath + "\\";

                infoContainer.overwriteIniValue("WebFile", "ProInfo", handwritingFolderDialog.SelectedPath + "\\");
            }
        }

        private void btnTKChoose_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog handwritingFolderDialog = new FolderBrowserDialog();
            handwritingFolderDialog.SelectedPath = tbTK.Text;
            if (handwritingFolderDialog.ShowDialog() == DialogResult.OK)
            {
                tbTK.Text = handwritingFolderDialog.SelectedPath + "\\";

                infoContainer.overwriteIniValue("WebFile", "TK", handwritingFolderDialog.SelectedPath + "\\");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            save();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            save();
            this.Close();
        }

        private void GlobalConfigWindow_Load(object sender, EventArgs e)
        {
            List<string> listTK = infoContainer.getValues("WebFile", "TK");
            List<string> listProInfo = infoContainer.getValues("WebFile", "ProInfo");
            List<string> listPaths = infoContainer.getValues("WebFile", "path(s)");
            List<string> listFrmInfo = infoContainer.getValues("WebFile", "FrmInfo");

            if ((listTK == null) || (listTK.Count == 0))
            {
                tbTK.Text = "";
            }
            else
            {
                tbTK.Text = listTK.ElementAt(0);
            }

            if ((listProInfo == null) || (listProInfo.Count == 0))
            {
                tbProInfo.Text = "";
            }
            else
            {
                tbProInfo.Text = listProInfo.ElementAt(0);
            }

            if ((listFrmInfo == null) || (listFrmInfo.Count == 0))
            {
                tbFrmInfo.Text = "";
            }
            else
            {
                tbFrmInfo.Text = listFrmInfo.ElementAt(0);
            }

            if (listPaths != null)
            {
                foreach (string path in listPaths)
                {
                    lbPaths.Items.Add(path);
                }
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.ListBox.SelectedObjectCollection items = lbPaths.SelectedItems;
            List<string> removeList = new List<string>();
            foreach (Object item in items)
            {
                removeList.Add(item.ToString());

                infoContainer.removeIniValue("WebFile", "path(s)", item.ToString());
            }
            foreach (string removeItem in removeList)
            {
                lbPaths.Items.Remove(removeItem);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog handwritingFolderDialog = new FolderBrowserDialog();
            handwritingFolderDialog.SelectedPath = tbTK.Text;
            if (handwritingFolderDialog.ShowDialog() == DialogResult.OK)
            {
                if (lbPaths.Items.Contains(handwritingFolderDialog.SelectedPath + "\\"))
                {
                    return;
                }

                lbPaths.Items.Add(handwritingFolderDialog.SelectedPath + "\\"); //= handwritingFolderDialog.SelectedPath + "\\";

                infoContainer.addIniValue("WebFile", "paht(s)", handwritingFolderDialog.SelectedPath + "\\");
            }
        }

        private void save()
        {
            if ((tbFrmInfo.Text == "") || (tbProInfo.Text == "") || (tbTK.Text == "") || (lbPaths.Items.Count == 0))
            {
                MessageBox.Show("Lack of Information!");
                return;
            }
            infoContainer.save();
        }
    }
}
